public class Offer16 {
    public double myPow(double x, int n) {
        if(Double.compare(x,0.0)==0){
            return 0.0;
        }
        if(Double.compare(x,1.0)==0){
            return 1.0;
        }
        if (Double.compare(x,-1.0)==0){
            return n%2==0 ? 1.0:-1.0;
        }
        if (n==Integer.MIN_VALUE){
            n = Integer.MIN_VALUE+1;
        }
        if (n>0){
            return nMulti(x,n);
        }else {
            if (x>0&&(-n)>100){
                return 0.0;
            }
            return 1/nMulti(x,-n);
        }

    }
//    public double nMulti(double x,int n){
//        double result = 1.0;
//        for (int i = 0; i < n; i++) {
//            result = x*result;
//        }
//        return result;
//    }

    // 递归二分幂
    public double nMulti(double x, int n) {
        if (n==0) return 1;
        if (n==1) return x;
        double result = myPow(x,n>>1);
        result*=result;
        if ((n&1)==1){
            result = result*x;
        }
        return result;
    }

    public static void main(String[] args) {
        Offer16 offer16 = new Offer16();
        offer16.myPow(2,-5);

    }

}
